<template>
  <el-card shadow="hover">
    <el-button
      type="primary"
      icon="el-icon-plus"
      size="small"
      @click="addTrademark"
      >添加品牌</el-button
    >
    <el-table
      :data="baseTrademarkList"
      border
      style="width: 100%"
      class="mt-20"
    >
      <el-table-column
        prop="index"
        label="序号"
        width="80"
        type="index"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称"> </el-table-column>
      <el-table-column prop="logoUrl" label="品牌LOGO">
        <!-- table使用图片 插槽-->
        <template v-slot="{ row }">
          <img :src="row.logoUrl" class="tm_img" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right" align="center">
        <template v-slot="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit-outline"
            size="mini"
            title="修改品牌"
            @click="editTrademark(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            title="删除品牌"
            @click="removeTrademark(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mt-20"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout=" prev, pager, next, jumper, ->,sizes,total"
      :total="total"
    >
    </el-pagination>

    <el-dialog
      :title="ruleForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        label-width="120px"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item label="品牌名称" prop="tmName">
          <el-input v-model="ruleForm.tmName" size="small"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <el-upload
            class="avatar-uploader"
            action="http://gmall-h5-api.atguigu.cn/admin/product/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            accept="image/jpeg,image/png"
          >
            <img
              v-if="ruleForm.logoUrl"
              :src="ruleForm.logoUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <template #tip>
              <div class="tip">只能上传jpg/png文件，且不超过200kb</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="small"
          >取 消</el-button
        >
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getBaseTrademarkListApi,
  addBaseTrademarkApi,
  removeBaseTrademarkApi,
  updateBaseTrademarkApi,
} from "@/api/product/trademark";
export default {
  name: "trademark",
  data() {
    return {
      total: 0,
      pageSize: 5,
      currentPage: 1,
      baseTrademarkList: [],
      dialogFormVisible: false,
      ruleForm: {
        tmName: "",
        logoUrl: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          // { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        logoUrl: [{ required: true, message: "请选择品牌LOGO" }],
      },
    };
  },
  mounted() {
    this.getBaseTrademarkList();
  },
  methods: {
    // 获取分页品牌列表
    async getBaseTrademarkList() {
      const res = await getBaseTrademarkListApi(
        this.currentPage,
        this.pageSize
      );
      const { records, total, size } = res.data;
      this.baseTrademarkList = records;
      this.total = total;
      this.pageSize = size;
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.getBaseTrademarkList();
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getBaseTrademarkList();
    },
    addTrademark() {
      this.dialogFormVisible = true;
      this.ruleForm.id = "";
      this.ruleForm.tmName = "";
      this.ruleForm.logoUrl = "";
    },
    editTrademark(row) {
      this.dialogFormVisible = true;
      this.ruleForm.id = row.id;
      this.ruleForm.tmName = row.tmName;
      this.ruleForm.logoUrl = row.logoUrl;
    },
    // 上传功能
    handleAvatarSuccess(res, file) {
      this.ruleForm.logoUrl = URL.createObjectURL(file.raw);
      this.$refs.ruleForm.clearValidate();
    },
    // 上传之前限制图片大小和格式
    /*
      图片大小 
        bit  位
        byte 字节  1字节 = 8位
        kb 千 1千 = 1024字节
        mb
        gb
        tb
    */
    beforeAvatarUpload(file) {
      // const isJPG = file.type === "image/jpeg";
      // const isLt2M = file.size / 1024 / 1024 < 2;
      const imgType = ["image/jpeg", "image/png"];
      const isLt2KB = file.size / 1024;
      if (!imgType.includes(file.type)) {
        this.$message.error("上传头像图片只能是 JPG 格式或 PNG 格式!");
        return false;
      }
      if (file.size / 1024 > 200) {
        this.$message.error("上传头像图片大小不能超过 200kb!");
        return false;
      }
      return imgType && isLt2KB;
    },
    // 提交，添加品牌和修改品牌
    async submit() {
      this.$refs.ruleForm.validate();
      const { id, tmName, logoUrl } = this.ruleForm;
      if (!tmName.trim()) return;
      if (!logoUrl.trim()) return;
      // console.log(this.ruleForm);
      // 发送添加请求
      if (id) {
        const res = await updateBaseTrademarkApi(this.ruleForm);
        this.$message.success("修改添加成功");
      } else {
        await addBaseTrademarkApi(this.ruleForm);
        this.$message.success("品牌添加成功");
      }

      this.getBaseTrademarkList();
      this.dialogFormVisible = false;
    },
    //删除品牌
    async removeTrademark(id) {
      await removeBaseTrademarkApi(id);
      this.$message.success("品牌删除成功");
      this.getBaseTrademarkList();
    },
  },
};
</script>

<style scoped>
.tm_img {
  width: 100px;
  height: 60px;
  line-height: 60px;
}

/* 上传组件 */
/* 修改子组件样式 */
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.tip {
  font-size: 12px;
}
</style>
